//
//  ES2Renderer.h
//  iKanKanTest
//
//  Created by dragon on 10/30/10.
//  Copyright Thunder Networking Inc 2010. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import <OpenGLES/EAGL.h>
#import <OpenGLES/EAGLDrawable.h>
#import <OpenGLES/ES2/gl.h>
#import <OpenGLES/ES2/glext.h>

#ifndef __STRUCTS_H__

#endif

// uniform index
enum {
	//    UNIFORM_TRANSLATE,
    UNIFORM_TEXTURE,		
    UNIFORM_LOGO_TEXTURE, 
	UNIFORM_SUBTITLE_TEXTURE, 
    UNIFORM_USELOGO,
    NUM_UNIFORMS
};

enum {
	TEXTURE_Y,
	TEXTURE_U,
	TEXTURE_V,
	NUM_YUV_UNIFORM
};

enum {
	Y_LEVEL,
	U_LEVEL,
	V_LEVEL,
	NUM_YUV_TEXTURES
};

typedef struct tagYUV_CTX
{
	char use_yuv;
	
	GLint uniform_yuv[NUM_YUV_UNIFORM];
	GLuint yuvTexture[NUM_YUV_TEXTURES];
	GLbyte* yuvTextureBuf;
	
	int raw_yuv_width;
	int raw_yuv_height;
	int sub_yuv_x;
	int sub_yuv_y;
	int sub_yuv_w;
	int sub_yuv_h;
	GLenum uv_pix_type;
} YUV_CTX;

@interface ES2Renderer : NSObject
{
@private
    EAGLContext *context;
    int play_now;
    GLfloat mask[16];
	int ps_changed;
    float default_brightness;
    float default_contrast;
    float default_saturation;
	
    // The OpenGL ES names for the framebuffer and renderbuffer used to render to this view
    GLuint defaultFramebuffer;
    GLuint colorRenderbuffer;
    
    // The pixel dimensions of the CAEAGLLayer
    GLint backingWidth;
    GLint backingHeight;

    GLuint program;
    GLuint program_array[4];

	/* OpenGL name for the sprite texture */
	GLuint spriteTexture;
	GLuint logo_texture;
	GLuint subtitle_texture;
	
	CGRect video_rect;
	CGRect video_src_rect;
	CGRect new_video_rect;
	CGRect new_video_src_rect;
	long need_update_rect;
	long size_mode;
	float vec_ratio;
	float sprite_tex_x;
	float sprite_tex_y;
	unsigned char *video_logo_buffer;
	CGRect video_logo_rect;
	CGSize video_logo_size;
	
	
	// Moved here from implemention
	unsigned char *sub_rgb;
	long texture_size_x;
	long texture_size_y;
	long sub_img_width;
	long sub_img_height;
	
	int texture_size_x_real;
	
	GLint *uniforms;
	GLint uniforms_array[4][NUM_UNIFORMS];
	GLfloat spriteVertices[8];
	GLfloat spriteTexcoords[8];
	GLfloat logoTexcoords[8];
	GLfloat subtitleTexcoords[8];
	GLfloat g_magnify_ratio;
	
	YUV_CTX yuv_ctx;

	int picture_linesize_y;
	int picture_linesize_u;
	int picture_linesize_v;
	
	int subtitle_width;
	int subtitle_height;
    
    void* lock;
}

//- (void)render;
- (BOOL)resizeFromLayer:(CAEAGLLayer *)layer;
- (long)set_video_rect:(CGRect*)video_rect src_rect_name:(CGRect*)src_rect;
- (long)update_video_rect:(CGRect*)video_rect src_rect_name:(CGRect*)src_rect;
- (long)set_video_logo:(CGRect*)logo_rect logo_buffer_name:(unsigned char *)logo_buffer logo_size_name:(CGSize*)logo_size;
- (long)set_video_subtitle:(CGRect*)subtitle_rect subtitle_buffer_name:(unsigned char *)subtitle_buffer subtitle_size_name:(CGSize*)subtitle_size;
- (long)open_video_subtitle;
- (long)close_video_subtitle;
- (long)set_video_ps:(long)brightness contrast_name:(long)contrast saturation_name:(long)saturation ;
- (long)set_size_mode:(long)size_mode_value;
- (long)get_size_mode;
- (long)refresh_video;
- (long)render_subtitle_pic:(unsigned char *)subtitle_pic_buffer;

/*@property (nonatomic) int picture_linesize_y;
@property (nonatomic) int picture_linesize_u;
@property (nonatomic) int picture_linesize_v;*/

@end

